<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="110px">
      <el-form-item label="单位名称" prop="unitName">
        <el-input
          v-model="queryParams.unitName"
          placeholder="请输入单位名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="统一信用代码" prop="creditCode">
        <el-input
          v-model="queryParams.creditCode"
          placeholder="请输入统一信用代码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="开户行" prop="bankName">
        <el-input
          v-model="queryParams.bankName"
          placeholder="请输入开户行"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="账号" prop="accountNumber">
        <el-input
          v-model="queryParams.accountNumber"
          placeholder="请输入账号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="税号" prop="taxNumber">
        <el-input
          v-model="queryParams.taxNumber"
          placeholder="请输入税号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="联系人" prop="contactPerson">
        <el-input
          v-model="queryParams.contactPerson"
          placeholder="请输入联系人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="电话" prop="contactPhone">
        <el-input
          v-model="queryParams.contactPhone"
          placeholder="请输入电话"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['business:partner:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['business:partner:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['business:partner:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['business:partner:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="partnerList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" align="center" type="index" />
      <el-table-column label="单位名称" align="center" prop="unitName" />
      <el-table-column label="统一信用代码/税号" align="center" prop="creditCode"  width="200">
        <template slot-scope="scope">
          <span >{{ scope.row.creditCode  }}</span><br>
          <span >{{  scope.row.taxNumber }}</span>
        </template>
      </el-table-column>
      <el-table-column label="开户行/账号" align="center" prop="bankName"  width="200">
        <template slot-scope="scope">
          <span >{{ scope.row.bankName  }}</span><br>
          <span >{{ scope.row.accountNumber }}</span>
        </template>
      </el-table-column>
      <el-table-column label="联系人/电话" align="center" prop="contactPerson" width="200">
        <template slot-scope="scope">
          <span >{{ scope.row.contactPerson }}</span><br>
          <span >{{ scope.row.contactPhone }}</span>
        </template>
      </el-table-column>

      <el-table-column label="备注" align="center" prop="remark" show-overflow-tooltip/>
      <el-table-column label="排序" align="center" prop="sortOrder" width="50"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-info"
            @click="handleInfo(scope.row)"
            v-hasPermi="['business:partner:query']"
          >查看</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['business:partner:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['business:partner:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改合作单位对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="50%" append-to-body :close-on-click-modal="false">
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
      <el-tabs v-model="activeName" >
        <el-tab-pane label="基本信息" name="first">
          <el-row :gutter="20">
            <el-col :span="12">
              <el-form-item label="单位名称" prop="unitName">
                <el-input v-model="form.unitName" placeholder="请输入单位名称" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="统一信用代码" prop="creditCode">
                <el-input v-model="form.creditCode" placeholder="请输入统一信用代码" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="开户行" prop="bankName">
                <el-input v-model="form.bankName" placeholder="请输入开户行" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="账号" prop="accountNumber">
                <el-input v-model="form.accountNumber" placeholder="请输入账号" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="税号" prop="taxNumber">
                <el-input v-model="form.taxNumber" placeholder="请输入税号" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="邮箱" prop="mail">
                <el-input v-model="form.mail" placeholder="请输入邮箱" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="联系人" prop="contactPerson">
                <el-input v-model="form.contactPerson" placeholder="请输入联系人" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="电话" prop="contactPhone">
                <el-input v-model="form.contactPhone" placeholder="请输入电话" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="是否公开" prop="isPublic">
                <el-radio v-model="form.isPublic"
                          v-for="dict in dict.type.sys_yes_no"
                          :key="dict.value"
                          :label="dict.value"
                         >{{dict.label}}</el-radio>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="排序字段" prop="sortOrder">
                <el-input-number v-model="form.sortOrder" :min="0" placeholder="请输入排序字段" />
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="备注" prop="remark">
                <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
              </el-form-item>
            </el-col>
          </el-row>
        </el-tab-pane>
        <el-tab-pane label="单位介绍" name="second">
          <el-form-item label="单位介绍">
            <editor v-model="form.unitDescription" :min-height="400"/>
          </el-form-item>
          <el-form-item label="相册" prop="albumImages">
            <image-upload v-model="form.albumImages"/>
          </el-form-item>
        </el-tab-pane>
      </el-tabs>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 合作单位信息对话框 -->
    <el-dialog :title="title" :visible.sync="openInfo" width="60%" append-to-body :close-on-click-modal="false">
      <el-descriptions border>
        <el-descriptions-item label="单位名称">{{ form.unitName }}</el-descriptions-item>
        <el-descriptions-item label="统一信用代码">{{ form.creditCode }}</el-descriptions-item>
        <el-descriptions-item label="开户行">{{ form.bankName }}</el-descriptions-item>
        <el-descriptions-item label="账号">{{ form.accountNumber }}</el-descriptions-item>
        <el-descriptions-item label="税号">{{ form.taxNumber }}</el-descriptions-item>
        <el-descriptions-item label="邮箱">{{ form.mail }}</el-descriptions-item>
        <el-descriptions-item label="联系人">{{ form.contactPerson }}</el-descriptions-item>
        <el-descriptions-item label="电话">{{ form.contactPhone }}</el-descriptions-item>
        <el-descriptions-item label="是否公开" >
          <dict-tag :options="dict.type.sys_yes_no" :value="form.isPublic"/>
        </el-descriptions-item>
        <el-descriptions-item label="备注" span="3">{{ form.remark}}</el-descriptions-item>
        <el-descriptions-item label="相册" span="3">
          <template v-for="i in albumImages">
            <image-preview  :src="i" :width="180" :height="180"/>
          </template>

        </el-descriptions-item>
        <el-descriptions-item label="介绍" span="3">
          <div v-html="form.unitDescription"></div>
        </el-descriptions-item>
      </el-descriptions>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">关闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listPartner, getPartner, delPartner, addPartner, updatePartner } from "@/api/business/partner";

export default {
  name: "Partner",
  dicts: ['sys_yes_no'],
  data() {
    return {
      activeName: 'first',
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 合作单位表格数据
      partnerList: [],
      albumImages: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      openInfo: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        unitName: null,
        creditCode: null,
        bankName: null,
        accountNumber: null,
        taxNumber: null,
        contactPerson: null,
        contactPhone: null,
        sortOrder: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        unitName: [
          { required: true, message: "单位名称不能为空", trigger: "blur" }
        ],
        creditCode: [
          { required: true, message: "统一信用代码不能为空", trigger: "blur" },
          { pattern: /^[0-9A-Z]{18}$/, message: "统一信用代码格式不正确", trigger: "blur" }
        ],
        contactPerson: [
          { required: true, message: "联系人不能为空", trigger: "blur" }
        ],
        contactPhone: [
          { required: true, message: "电话不能为空", trigger: "blur" },
          { pattern: /^(0\d{2,3}-\d{7,8}|\d{11})$/, message: "电话格式不正确", trigger: "blur" }
        ],
        mail: [
          { type: 'email', message: "邮箱格式不正确", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询合作单位列表 */
    getList() {
      this.loading = true;
      listPartner(this.queryParams).then(response => {
        this.partnerList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.openInfo = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        partnerId: null,
        unitName: null,
        creditCode: null,
        bankName: null,
        accountNumber: null,
        taxNumber: null,
        contactPerson: null,
        contactPhone: null,
        sortOrder: 0,
        unitDescription: null,
        albumImages: null,
        isPublic: 'N',
        mail: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
        delFlag: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.partnerId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加合作单位";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const partnerId = row.partnerId || this.ids
      getPartner(partnerId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改合作单位";
      });
    },
      /** 查看按钮操作 */
      handleInfo(row) {
        this.reset();
        const partnerId = row.partnerId || this.ids
        getPartner(partnerId).then(response => {
          this.form = response.data;
          this.albumImages =  response.data.albumImages.split(",");
          this.openInfo = true;
          this.title = "合作单位信息";
        });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.partnerId != null) {
            updatePartner(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addPartner(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const partnerIds = row.partnerId || this.ids;
      this.$modal.confirm('是否确认删除合作单位编号为"' + partnerIds + '"的数据项？').then(function() {
        return delPartner(partnerIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('business/partner/export', {
        ...this.queryParams
      }, `合作单位信息_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
